package tp.pr2;

import java.util.Scanner;

public class RobotEngine {
	private Place actualPlace;
	private Direction actualDirection;
	private City city;
    private int fuel;
    private int recycledMaterial;
    private ItemContainer inventory;
    
    public RobotEngine(City cityMap,
            Place initialPlace,
            Direction dir)
    {
    actualPlace=initialPlace;
   	actualDirection=dir;
   	city=cityMap;
   	fuel=50;
   	recycledMaterial=0;
    inventory = new ItemContainer();
    }
   
    public void addFuel(int fuel)
    {    
    this.fuel=this.fuel+fuel;	
    }
    
    public void addRecycledMaterial(int weight)
    {
    	recycledMaterial=recycledMaterial+weight;
    }
      
    public int getRecycledMaterial()
    {
    	return recycledMaterial;
    } 
    
    public int getFuel()
    {
    	return fuel;
    }
    
    public java.lang.String statsToString()
    {
    	String LINE_SEPARATOR = System.getProperty("line.separator");
		return( "   * My power is " + 			
    	       this.getFuel())	+	
               LINE_SEPARATOR +
               "   * My recycled material is: "+
               this.getRecycledMaterial();
               	
    }
    
    public Street getHeadingStreet()
    {
    	return city.lookForStreet(actualPlace, actualDirection);
    }   
    
    public void startEngine()
    {
		Instruction instruction = new Instruction();
		Scanner sc=new Scanner(System.in);		
		showText();

		while(!actualPlace.isSpaceship() && instruction.getAction()!=Action.QUIT && fuel>0)
		{				
		 System.out.print("WALL·E > ");		 
		 String order =sc.nextLine();
		 instruction=Interpreter.generateInstruction(order);
		 
		 if(instruction.isValid())
		 	{
			 switch(instruction.getAction())
				 {
				 case HELP: Help(instruction);break;
				 case QUIT: Quit(instruction);break;
				 case MOVE: changePlace();break;
				 case SCAN: Scan(instruction);break;				         
				 case TURN: Turn(instruction);break;
				 case OPERATE:Operate(instruction);break;
				 case PICK:Pick(instruction);break;
				 default:break;
				 }
		 	}
		 else
		 	{
			 System.out.println("WALL·E says: I do not understand. Please repeat");
		 	}		
		if(fuel==0)System.out.println("WALL·E says: I run out of fuel. I cannot move. Shutting down...");
		}
	sc.close();	
   }
    
    private void Help(Instruction instruction)
    	{
    	System.out.println(Interpreter.interpreterHelp());
    	}
    
    private void Quit(Instruction instruction)
    	{
    	System.out.println("WALL·E says: I have communication problems. Bye bye");
    	}
    
    private void Turn(Instruction instruction)
    {
 	  actualDirection=Direction.rotate(actualDirection,instruction.getRotation());
 	  fuel--;
 	  System.out.println(statsToString());
	  System.out.println("WALL·E is looking at direction " + actualDirection.toString());
    }
    
    private void Scan(Instruction instruction)
    	{
    	 if(instruction.getId().equals(""))
    	 	{
    		 if(inventory.numberOfItems()==0) 
    			 System.out.println("WALL·E says: My inventory is empty");
    		 else if(inventory.numberOfItems()>0)
    			    {
    			    System.out.println("WALL·E says: I am carrying the following items");
    			    System.out.println(inventory.toString());
    			    }
    	 	}
    	 else
    	    {
    		 if(inventory.getItem(instruction.getId())==null)
    			 System.out.println("WALL·E says: I have not such object");
    	     else System.out.println("WALL·E says: "+inventory.getItem(instruction.getId()).toString());
    	    }
    	}
  
    private void Operate(Instruction instruction)
    	{
    	if(inventory.getItem(instruction.getId())==null)
    		System.out.println("WALL·E says: I have problems using the object "+instruction.getId());
    	else
    		{
    		if(inventory.getItem(instruction.getId()).canBeUsed()==false)
    			System.out.println("WALL·E says: I have problems using the object "+instruction.getId());
    		else
    			{
    			int fuelAux=fuel;
    			int recycledMaterialAux=recycledMaterial;    			
    			if(inventory.getItem(instruction.getId()).use(this, actualPlace)==false)
    				System.out.println("WALL·E says: I have problems using the object "+instruction.getId());
    			if(fuelAux!=fuel || recycledMaterialAux!=recycledMaterial)
    			if(!inventory.getItem(instruction.getId()).canBeUsed())
    			   {
    				System.out.println("WALL·E: What a pity! I have no more " +
    				   instruction.getId()+
    				   " in my inventory");
    				inventory.pickItem(instruction.getId());
    			   }
    			}  		
    		}
    	}

    
    private void Pick(Instruction instruction)
    	{
    	 if(inventory.getItem(instruction.getId())!=null) 
    		   System.out.println("WALL·E says: I am stupid! I already have the object "+ instruction.getId());
    	 else
    		 {
    		 Item itemAux=actualPlace.pickItem(instruction.getId());
    		 if(itemAux==null)
    		     System.out.println("WALL·E says: Ooops, this place has not the object "+instruction.getId());
    	     else{  
    		      inventory.addItem(itemAux);
    		      System.out.println("WALL·E says: I am happy! Now I have  "+instruction.getId());
    	         }
    		 }
    	}
    
    
    private boolean changePlace()
	{
     if(city.lookForStreet(actualPlace, actualDirection)==null)
        {
    	 System.out.println("WALL·E says: There is no street in direction " + actualDirection.toString());
    	 return false;
        }
     if(!city.lookForStreet(actualPlace, actualDirection).isOpen())
     	{
    	 System.out.println("WALL·E says: Arrggg, there is a street but it is closed!");
    	 return false;
     	}   
     System.out.println("WALL·E says: Moving in direction " + actualDirection.toString());
     actualPlace=city.lookForStreet(actualPlace, actualDirection).nextPlace(actualPlace);
     fuel=fuel-5;
     System.out.println(actualPlace.toString());
     System.out.println(statsToString());
     System.out.println("WALL·E is looking at direction " + actualDirection.toString());
     if(actualPlace.isSpaceship())System.out.println("WALL·E says: I am at my spaceship. Shutting down... Bye bye");
     return true;
	}	    
    
    private void showText()
    {
        System.out.println(actualPlace.toString());	
		System.out.println("");
		System.out.println(statsToString());
		System.out.println("WALL·E is looking at direction " + actualDirection.toString());
    }
}     

